#include "ipconvertertest.h"
#ifdef Q_OS_WIN
#include <windows.h>
#endif
#include "common/logger/logger.h"
IpConverterTest::IpConverterTest(QObject* parent)
{
}
IpConverterTest::~IpConverterTest()
{
}
void IpConverterTest::initTestCase()
{
}
void IpConverterTest::tstIpConverter()
{
	// 正常有效 IPv4
	std::vector<std::string> validIps = {
			"0.0.0.0",
			"255.255.255.255",
			"127.0.0.1",
			"192.168.1.1",
			"10.0.0.1",
			"172.16.0.1",
			"8.8.8.8"};

	for (const auto& ip: validIps)
	{
		try
		{
			int64_t ipNum = IpConverter::ipAddress2uint64t(ip);
			std::string ipBack = IpConverter::int64t2IpAddress(ipNum);
			LOG_INFO("Valid IP: ", ip, " -> int64_t: ", ipNum, " -> IP back: ", ipBack);
		}
		catch (const std::exception& e)
		{
			LOG_ERROR("Valid IP test failed: ", ip, " -> Exception: ", e.what());
		}
	}

	// 边界值测试
	std::vector<int64_t> boundaryValues = {0, 1, 3232235777, 4294967295};
	for (auto val: boundaryValues)
	{
		try
		{
			std::string ip = IpConverter::int64t2IpAddress(val);
			int64_t valBack = IpConverter::ipAddress2uint64t(ip);
			LOG_INFO("Boundary int64_t: ", val, " -> IP: ", ip, " -> int64_t back: ", valBack);
		}
		catch (const std::exception& e)
		{
			LOG_ERROR("Boundary int64_t test failed: ", val, " -> Exception: ", e.what());
		}
	}

	// 常见子网、广播地址测试
	std::vector<std::string> specialIps = {
			"192.168.1.0",
			"192.168.1.255",
			"10.0.0.0",
			"10.0.0.255"};
	for (const auto& ip: specialIps)
	{
		try
		{
			int64_t ipNum = IpConverter::ipAddress2uint64t(ip);
			std::string ipBack = IpConverter::int64t2IpAddress(ipNum);
			LOG_INFO("Special IP: ", ip, " -> int64_t: ", ipNum, " -> IP back: ", ipBack);
		}
		catch (const std::exception& e)
		{
			LOG_ERROR("Special IP test failed: ", ip, " -> Exception: ", e.what());
		}
	}

	// 错误输入测试（异常捕获）
	std::vector<std::string> invalidIps = {
			"256.0.0.1",
			"192.168.1",
			"192.168.1.1.1",
			"abc.def.ghi.jkl",
			"-1.0.0.0"};
	for (const auto& ip: invalidIps)
	{
		try
		{
			int64_t ipNum = IpConverter::ipAddress2uint64t(ip);
			LOG_INFO("Invalid IP (should fail): ", ip, " -> int64_t: ", ipNum);
		}
		catch (const std::exception& e)
		{
			LOG_ERROR("Invalid IP correctly caught exception: ", ip, " -> ", e.what());
		}
	}

	// 随机常用公网 IP 测试
	std::vector<std::string> publicIps = {
			"1.1.1.1",
			"8.8.4.4",
			"208.67.222.222",
			"114.114.114.114"};
	for (const auto& ip: publicIps)
	{
		try
		{
			int64_t ipNum = IpConverter::ipAddress2uint64t(ip);
			std::string ipBack = IpConverter::int64t2IpAddress(ipNum);
			LOG_INFO("Public IP: ", ip, " -> int64_t: ", ipNum, " -> IP back: ", ipBack);
		}
		catch (const std::exception& e)
		{
			LOG_ERROR("Public IP test failed: ", ip, " -> Exception: ", e.what());
		}
	}
}
void IpConverterTest::cleanupTestCase()
{
}


int main(int argc, char** argv)
{

	// 设置Windows控制台编码
#ifdef Q_OS_WIN
	// 设置控制台输出代码页为 UTF-8
	SetConsoleOutputCP(CP_UTF8);
	// 可选：设置控制台输入为 UTF-8
	SetConsoleCP(CP_UTF8);
#endif
	QApplication app(argc, argv);
	IpConverterTest tc;
	return QTest::qExec(&tc, argc, argv);// qExec 会把失败信息打印到 stdout
}